<template>
  <div class="detailbox">
    <van-nav-bar left-arrow>
      <template #right>
        <van-icon name="ellipsis" size="18" />
      </template>
    </van-nav-bar>
    <div class="pic_">
      <van-cell  @click="showPopup">
        <div class="swip">
          <van-swipe @change="onChange">
            <van-swipe-item class="imgitem">
              <img :src="goods.img1" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem">
              <img :src="goods.img2" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem">
              <img :src="goods.img3" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem">
              <img :src="goods.img4" alt />
            </van-swipe-item>
            <template #indicator>
              <div class="custom-indicator">{{ current + 1 }}/4</div>
            </template>
          </van-swipe>
        </div>
      </van-cell>
      <!-- 点击图片放大 -->
      <van-popup v-model="show1">
        <div class="swip">
          <van-swipe>
            <van-swipe-item class="imgitem1">
              <img :src="goods.img1" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem1">
              <img :src="goods.img2" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem1">
              <img :src="goods.img3" alt />
            </van-swipe-item>
            <van-swipe-item class="imgitem1">
              <img :src="goods.img4" alt />
            </van-swipe-item>
            <template #indicator>
              <div class="custom-indicator">{{ current + 1 }}/4</div>
            </template>
          </van-swipe>
        </div>
      </van-popup>

      <div class="desc_">
        <p>
          <span class="price_">￥{{goods.originprice}}</span>
          <span class="sales_">销量{{ goods.sales }}</span>
        </p>
        <p class="title_">
          <span class="brand">{{ goods.brand }}</span>
          <span class="category">{{ goods.category }}</span>
          <span class="name">{{goods.proname}}</span>
        </p>
      </div>
      <div class="place">
        <van-field
          v-model="fieldValue"
          is-link
          readonly
          label="地区"
          placeholder="请选择所在地区"
          @click="show = true"
        />
        <van-popup v-model="show" round position="bottom">
          <van-cascader
            v-model="cascaderValue"
            title="请选择所在地区"
            :options="options"
            @close="show = false"
            @finish="onFinish"
          />
        </van-popup>
      </div>
      <div class="like_">
        <b>猜你喜欢</b>
        <ul>
          <li v-for="obj in like" :key="obj.proid" @click="goDetail(obj.proid)">
            <img :src="obj.img1" alt />
            <p class="like-name">{{ obj.proname }}</p>
            <p class="like-price">￥{{ obj.originprice }}</p>
          </li>
        </ul>
      </div>
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" color="#ee0a24" />
        <van-goods-action-icon icon="cart-o" text="购物车" />
        <van-goods-action-icon icon="star" text="已收藏" color="#ff5000" />
        <van-goods-action-button type="warning" text="加入购物车" color="#ff5000" @click="addgoods()"/>
      </van-goods-action>
    </div>
  </div>
</template>

<script >
import axios from "axios";

export default {
  name: "GoodsDetail",
  data() {
    return {
      current: 0,
      goods: {},
      like: [],
      show1: false,
  
      //地址
      show: false,
      fieldValue: "",
      cascaderValue: "",
      // 选项列表，children 代表子选项，支持多级嵌套
      options: [
        {
          text: "浙江省",
          value: "330000",
          children: [{ text: "杭州市", value: "330100" }]
        },
        {
          text: "江苏省",
          value: "320000",
          children: [{ text: "南京市", value: "320100" }]
        }
      ]
    };
  },
  created() {
    let proid = this.$route.params.goodsid;
    axios({
      url: "http://121.89.205.189:3001/api/pro/detail/" + proid
    }).then(res => {
      if (res.data.code == 200) {
        this.goods = res.data.data;
        console.log(this.goods);
      }
    });
    axios({
      url: "http://121.89.205.189:3001/api/pro/recommendlist"
    }).then(res => {
      if (res.data.code == 200) {
        this.like = res.data.data;
      }
    });
  },
  methods: {
    onClickLeft() {},
    onClickRight() {},
    onChange(index) {
      this.current = index;
    },
    goDetail(goodsid) {
      this.$router.push("/goodsdetail/" + goodsid);
      window.location.reload();
    },
    // enlarge(e){
    //   let ele =e.target;
    //   ele.style.position="absolute";
    //   ele.style.top="100px";

    // },

    //地址
    // 全部选项选择完毕后，会触发 finish 事件
    onFinish({ selectedOptions }) {
      this.show = false;
      this.fieldValue = selectedOptions.map(option => option.text).join("/");
    },

    //点击图片放大
    showPopup() {
      this.show1 = true;
    },
    addgoods(){
      let token=localStorage.getItem("token");
      let userid=localStorage.getItem("userid");
      let proid1 = this.$route.params.goodsid;
      console.log(token);
      axios({
        url:"http://121.89.205.189:3001/api/cart/add",
        method:"post",
        token:"token",
        data:`userid=${userid}&proid=${proid1}&num=1`,
      }).then(res=>{
        if(res.data.code == 200){
          alert("加入购物车成功！")
        }
      })
    }
  }
};
</script>

<style scoped>
.detailbox {
  padding: 0.3125rem;
}
.custom-indicator {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 2px 5px;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.1);
}
img {
  width: 100%;
  height: 100%;
  height: 25rem;
}
.brand,
.category {
  display: inline-block;
  padding: 0 0.3125rem;
  color: white;
  font-weight: 700;
  border-radius: 0.3125rem;
  margin-right: 0.3125rem;
}
.price_ {
  font-size: 1.875rem;
  color: orange;
}
.sales_ {
  float: right;
}
.brand {
  background-color: #ee0a24;
}
.category {
  background-color: #1989fa;
}
.like_ {
  background-color: white;
  padding: 0.625rem;
  overflow: hidden;
}
ul {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
li {
  width: 48%;
  margin-bottom: 0.625rem;
  /* height: 25rem; */
}
li img {
  width: 100%;
  height: 9.8125rem;
}
.like-name {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0;
}
.like-price {
  color: #ee0a24;
  padding: 0;
}
.imgitem1 img{
  width:21.875rem;
  paddinng:0px;
}
</style>